<!-- subpkg_archive/list/index.vue -->
<script lang="ts" setup>
  import { onMounted, ref } from 'vue'
  import type { PatientItem } from '@/types/archive'
  import { delArchiveAPI, getArchiveListAPI } from '@/api'
  import { onShow } from '@dcloudio/uni-app'
  import { useConsultStore } from '@/store/consult'
  import { PAGE_PATH } from '@/constants'

  const store = useConsultStore() // 使用pinia
  const props = defineProps<{ type: string }>()

  if (props.type === 'consult') {
    uni.setNavigationBarTitle({ title: '选择患者信息' })
  }
  const swipeOptions = ref([
    {
      text: '删除',
      style: {
        backgroundColor: '#dd524d',
        color: '',
        fontSize: '',
      },
    },
  ])
  const list = ref<PatientItem[]>([])

  // onMounted(() => {
  //   getArchiveList()
  // })

  onShow(() => {
    getArchiveList()
  })
  // 获取患者列表
  const getArchiveList = async () => {
    list.value = await getArchiveListAPI()
    if (props.type === 'consult') {
      // 此时说明在极速问诊的场景下 并且当前没有被选中的患者 才需要把默认的患者当成选中患者
      if (!store.illnessInfo.patientId) {
        // 此时说明没有选中患者
        // 把默认患者设置成选中患者
        store.illnessInfo.patientId = list.value.find((item) => item.defaultFlag === 1)?.id || '' // 找到谁是默认的 如果还没有默认就给空字符串
      }
    }
  }
  const switchClick = (id: string) => {
    uni.showModal({
      title: '确定删除该患者档案吗',
      async success(res) {
        if (res.confirm) {
          await delArchiveAPI(id)
          getArchiveList()
        }
      },
    })
  }
  // 点击患者的id
  const selectPatient = (id: string) => {
    store.illnessInfo.patientId = id // 记录当前患者的id
  }
  // 去支付页面
  const goToPay = () => {
    if (!store.illnessInfo.patientId) {
      uni.showToast({ title: '请先选择患者', icon: 'none' })
      return
    }
    uni.navigateTo({
      url: PAGE_PATH.CONSULT_PAY_PAGE,
    })
  }
</script>

<template>
  <scroll-page>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>

      <uni-swipe-action>
        <uni-swipe-action-item
          @click="switchClick(item.id)"
          :key="item.id"
          v-for="item in list"
          :right-options="swipeOptions"
        >
          <view
            :class="{ active: item.id === store.illnessInfo.patientId }"
            @click="selectPatient(item.id)"
            class="archive-card"
          >
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">{{
                item.idCard.replace(/^(.{6}).+(.{4})$/, '$1********$2')
              }}</text>
              <text class="default" v-if="item.defaultFlag === 1">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.gender === 1 ? '男' : '女' }}</text>
              <text class="age">{{ item.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/form/index?id=${item.id}`"
            >
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/form/index">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
      <!-- 如果是极速问诊 要显示下一步 -->
      <view v-if="props.type === 'consult'" class="next-step">
        <button @click="goToPay" class="uni-button">下一步</button>
      </view>
    </view>
  </scroll-page>
</template>

<style lang="scss">
  @import './index.scss';
</style>
